package com.tm.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tm.dto.OrderDTO;
import com.tm.entity.DishEntity;
import com.tm.entity.OrderEntity;
import com.tm.service.OrderService;
import com.tm.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author huangp
 * @create 2023-04-09 14:05
 */
@RestController
@RequestMapping("order")
@CrossOrigin
public class OrderController {

    @Autowired
    private OrderService orderService;

    @GetMapping("page")
    public Result getOrderDetailPage(@RequestParam(required = false,defaultValue = "1") Integer page ,
                                     @RequestParam(required = false,defaultValue = "10") Integer pageSize,
                                     OrderDTO orderDTO){
        QueryWrapper<OrderEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(orderDTO.getNumber()!=null,"number",orderDTO.getNumber());
        queryWrapper.ge(orderDTO.getBeginTime() != null,"order_time",orderDTO.getBeginTime());
        queryWrapper.le(orderDTO.getEndTime() != null,"order_time",orderDTO.getEndTime());
        Page<OrderEntity> page1 = new Page<>(page, pageSize);
        Page<OrderEntity> list = orderService.page(page1,queryWrapper);
        return Result.success(list);

    }

    @GetMapping("/{id}")
    public Result queryOrderDetailById(@PathVariable("id") Long id){

        OrderEntity list = orderService.getById(id);
        return Result.success(list);

    }

    @PutMapping
    public Result editOrderDetail(@RequestBody OrderEntity orderEntity){
        /*UpdateWrapper<OrderEntity> UpdateWrapper = new UpdateWrapper<>();
        UpdateWrapper.eq("id",orderEntity.getId());*/
        Boolean flag = orderService.updateById(orderEntity);
        return Result.success(flag);
    }

}
